<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/9 0009
 * Time: 上午 10:28
 */

namespace app\admin\controller;


use controller\BasicAdmin;
use service\DataService;
use service\ToolsService;
use think\Db;

class IndexBlock extends BasicAdmin
{
    public function index(){
        $this->title = '首页内容';
        $db = Db::name('IndexBlock')->where(['is_deleted'=>1])->where('id','<>',10)->order('sort asc,id asc');
        return parent::_list($db,false);
    }

    public function _index_data_filter(&$data){
        foreach ($data as &$vo){
            $vo['ids'] = join(',', ToolsService::getArrSubIds($data, $vo['id']));
        }
        $data = ToolsService::arr2table($data);
    }

    public function add(){
        return $this->_form('IndexBlock','form');
    }

    public function content(){
        if ($this->request->isPost()){
            $post = $this->request->post();
            $post['content'] = json_encode($post['content'],320);
            DataService::save('IndexBlockContent',$post);
            $this->success('保存成功！','');
        }

        $id = $this->request->get('id');
        $index_block = Db::name('IndexBlock')->field('title,template')->find($id);
        $vo = Db::name('IndexBlockContent')->where('pid',$id)->find();
        $vo['content'] = json_decode($vo['content'], true);
        $this->assign([
            'title'=>$index_block['title'],
            'vo'=>$vo,
            'pid'=>$id
        ]);
        return view($index_block['template']);
    }

    public function lists(){
        $id = $this->request->get('id');
        if ($this->request->isPost()){
            $post = $this->request->post();
            foreach ($post as $k=>$v){
                DataService::save('IndexBlockContent',['content'=>json_encode($v,320),'pid'=>$k,'type'=>'nav'],'pid',['pid'=>$k,'type'=>'nav']);
            }
            $this->success('保存成功！','');
        }
        $index_block = Db::name('IndexBlock')->field('title,template')->find($id);
        $db = Db::name('IndexBlock')->field('id,title')->where(['is_deleted'=>1,'pid'=>$id])->order('sort asc,id asc')->select();
        foreach ($db as $k=>$v){
            $content = Db::name('IndexBlockContent')->where(['pid'=>$v['id'],'type'=>'nav'])->value('content');
            $db[$k]['content'] = json_decode($content,true);
        }

        $this->assign([
            'title'=>$index_block['title'],
            'db'=>$db
        ]);
        return view();
    }

    public function cate(){
        $id = $this->request->get('id');
        if ($this->request->isPost()){
            $post = $this->request->post();
            DataService::save('IndexBlockContent',['content'=>json_encode($post,320),'pid'=>$id],'pid',['type'=>null]);
            $this->success('保存成功！','');
        }
        $index_block = Db::name('IndexBlock')->field('title,template')->find($id);
        $db = Db::name('IndexBlockContent')->where(['pid'=>$id,'is_deleted'=>1,'type'=>null])->find();
        $db['content'] = json_decode($db['content'],true);

        $this->assign([
            'title'=>$index_block['title'],
            'db'=>$db['content']
        ]);
        return view($index_block['template']);
    }
}